package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Custurn;
import cn.wolfcode.crm.query.impl.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("customer")
public class CustomerController {
    @Autowired
    ICustomerService customerService;

    @RequestMapping("customer")
    @RequiresPermissions(value = {"customer:view","潜在客户页面"},logical= Logical.OR)
    public String view() {
        return "customer";
    }

    @RequestMapping("resource")
    @RequiresPermissions(value = {"customer:view","客户资源池页面"},logical= Logical.OR)
    public String customer() {
        return "resource";
    }

    @RequestMapping("official")
    @RequiresPermissions(value = {"customer:view","客户资源池页面"},logical= Logical.OR)
    public String official() {
        return "official";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public List<Customer> getAll(CustomerQuery query) {
        return customerService.selectAll(query);
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public List<Customer> selectAll() {
        return customerService.getAll();
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"customer:save","保存客户"},logical= Logical.OR)
    @ResponseBody
    public JSONResult saveOrUpdate(Customer customer){
        try {
            customerService.saveOrUpdate(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id){
        try {
            customerService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }
    @RequestMapping("setInchargeuser")
    @RequiresPermissions(value = {"customer:save","保存客户"},logical= Logical.OR)
    @ResponseBody
    public JSONResult setInchargeuser(Custurn custurn){
        try {
            customerService.setInchargeuser(custurn);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"列表更新失败");
        }
        return new JSONResult(true,"恭喜您执行成功");
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(CustomerQuery qo) {
        return customerService.query(qo);
    }

    @RequestMapping("moveResource")
    @ResponseBody
    public JSONResult moveResource(Long id) {
        try {
            customerService.moveResource(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"移入资源池失败");
        }
        return new JSONResult(true,"该客户成功移入资源池");
    }
    @RequestMapping("setOfficial")
    @ResponseBody
    public JSONResult setOfficial(Long id,Long chargeId) {
        try {
            customerService.setOfficial(id,chargeId);
        } catch (Exception e) {
            e.printStackTrace();
            return new JSONResult(false,"移入资源池失败");
        }
        return new JSONResult(true,"该客户成功移入资源池");
    }

}
